package com.ego.dubbo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ego.dubbo.service.ContentDubboService;
import com.ego.mapper.ContentMapper;
import com.ego.pojo.Content;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.List;

@DubboService
public class ContentDubboServiceImpl implements ContentDubboService {

    @Autowired
    private ContentMapper contentMapper;


    @Override
    public List<Content> selectContentByPage(long categoryId, int pageNum, int pageSize) {
        Page<Content> condition = new Page<>(pageNum, pageSize);
        QueryWrapper wrapper = new QueryWrapper();
        if (categoryId > 0) {
            wrapper.eq("category_id", categoryId);
        }
        wrapper.orderByDesc("updated");
        Page<Content> page = contentMapper.selectPage(condition, wrapper);
        return page.getRecords();
    }

    @Override
    public long selectTotal(long categoryId) {
        QueryWrapper wrapper = new QueryWrapper();
        if (categoryId > 0) {
            wrapper.eq("category_id", categoryId);
        }
        return contentMapper.selectCount(wrapper);
    }

    @Override
    public int saveContent(Content content) {
        return contentMapper.insert(content);
    }

    @Override
    public int updateContent(Content content) {
        return contentMapper.updateById(content);
    }

    @Override
    public int deleteContentByIds(Long[] ids) {
        int count = contentMapper.deleteBatchIds(Arrays.asList(ids));
        if (count == ids.length){
            return 1;
        }
        return 0;
    }

}
